﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Management_Addcourse : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Enabled = false;
        TextBox2.Enabled = false;
        TextBox3.Enabled = false;
        TextBox4.Enabled = false;
        TextBox5.Enabled = false;
        TextBox6.Enabled = false;
        TextBox8.Enabled = false;
        TextBox9.Enabled = false;
        FileUpload2.Enabled = false;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox1.Enabled = true;
        TextBox2.Enabled = true;
        TextBox3.Enabled = true;
        Session["flag"] = 1;
    }
    protected void  Button1_Click(object sender, EventArgs e)
    {
        if (Session["flag"].ToString() == "1")
        {
            string did = TextBox1.Text;
            string dname = TextBox2.Text;
            string dintroduction = TextBox3.Text;
            DBaccess db = new DBaccess();
            SqlDataReader read = db.GetReader("select count(*) from mr_department where did='" + did + "'");
            read.Read();
            if (int.Parse(read.GetValue(0).ToString()) == 0)
            {
                string sql = "insert into mr_department(did,dname,introduction) values('" + did + "','" + dname + "','" + dintroduction + "')";
                if (db.ExecSql(sql) != 0)
                {
                    Response.Write("<script>alert('上传成功!');</script>");
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                    TextBox3.Text = "";

                }
                else
                {
                    Response.Write("<script>alert('上传出错!');</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('该学院号已存在!');</script>");
            }
        }
        else if (Session["flag"].ToString() == "2")
        {
            DBaccess db = new DBaccess();
            string cid = TextBox4.Text;
            string cname = TextBox5.Text;
            string cintroduction = TextBox6.Text;
            string dnum = TextBox8.Text;
            string keyword = TextBox9.Text;
            string kzm1 = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
            System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload2.PostedFile.InputStream);

            int height = image.Height;
            int width = image.Width;
            if (height == 64 && width == 64)
            {
                SqlDataReader read = db.GetReader("select count(*) from mr_course where cid='" + cid + "'");
                read.Read();
                if (int.Parse(read.GetValue(0).ToString()) == 0)
                {
                    string time1 = DateTime.Now.ToFileTime().ToString();
                    string time = DateTime.Now.ToString();
                    string picture = cname + time1;
                    string path1 = "Picture\\" + picture + kzm1;
                    string fullpath1 = MapPath("..") + "\\" + path1;
                    string resourcepath1 = "~\\Picture\\" + picture + kzm1;
                    FileUpload2.PostedFile.SaveAs(fullpath1);
                    string sql = "insert into mr_course(cid,cname,introduction,department,keyword,logo) values('" + cid + "','" + cname + "','" + cintroduction + "','" + dnum + "','" + keyword + "','" + resourcepath1 + "')";
                    if (db.ExecSql(sql) != 0)
                    {
                        Response.Write("<script>alert('上传成功!');</script>");
                        TextBox4.Text = "";
                        TextBox5.Text = "";
                        TextBox6.Text = "";
                        TextBox8.Text = "";
                        TextBox9.Text = "";

                    }
                    else
                    {
                        Response.Write("<script>alert('上传出错!');</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('该课程号已存在!');</script>");
                }
            }

        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        TextBox4.Enabled = true;
        TextBox5.Enabled = true;
        TextBox6.Enabled = true;
        TextBox8.Enabled = true;
        TextBox9.Enabled = true;
        FileUpload2.Enabled = true;
        Session["flag"] = 2;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["search"] = TextBox7.Text;
        Response.Redirect("./Search.aspx");
    }
}
